﻿using System;
using System.IO;

namespace Rites
{
    public static class Constants
    {
        public const string BUILD_VERSION = "Alpha 1.1.0"; // This is the build number which gets displayed on screen during development
                                                    // Should be incremented or changed when we send a version out for testing or with major changes
                                                    // This helps us with screenshots of bugs, so we know which build it was is
                                                    // Changes should be checked in with this version tag so we can recreate the problematic build

        public const string BOM = "~~~"; //Beginning of message
        public const string EOM = "^^^"; //End of message
        public const int PORT = 12324;
        public const int READBUFFERSIZE = 256; //Used for network comms
        public const int KEYBOARD_BUFFERSIZE = 256; //Keyboard input

        public const int VIEW_TILES_ACROSS = 15; //Map tiles to display across
        public const int VIEW_TILES_DOWN = 9; //Map tiles to display down
    }

    public enum eMonsterType
    {
        EvilWizard = 0
    }

    public enum eMovementDiretions
    {
        Up = 0,
        Down = 1,
        Left = 2,
        Right = 3
    }

    public enum eGameState
    {
        Quitting = 0,
        Offline = 1,
        GetIPAddress = 2,
        GetCharName = 3,
        GetPassword = 4,
        Playing = 5
    }

    public enum eTerrainTypes
    {
        Grass = 0,
        Pebbles = 1
    }

    public static class Util
    {
        public static Rites.GameObjects.Player LoadPlayer(string playerName)
        {
            //TODO: From file, hardcoded probably won't work...
            //Check password when loading up here too I guess

//            if (playerName.ToLower() == "bob")
  //          {
                Rites.GameObjects.Player plr = new Rites.GameObjects.Player();
                plr.Name = playerName;
                plr.X = 7;
                plr.Y = 7;
                return plr;
    //        }
      //      else
        //        return null;
        }

        public static void Log(string logString)
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText("RitesClientLog.txt");
                sw.WriteLine(System.DateTime.Now.ToString() + " -- " + logString);
                sw.Flush();
                sw.Close();
            }
            catch (Exception)
            {
                // This is PROBABLY a file acccess contention error because the server is multithreaded
                // Many events which log to this file asynchronously
                // Probably...
                
                //TODO: This should be handled somehow?
            }
        }

        public static MemoryStream CreateMessage(Rites.Networking.RitesProtocol messageType, string paramString, out long messageID, long replyID)
        {
            //Util.Log("CreateMessage: " + Enum.GetName(typeof(Rites.Networking.RitesProtocol), messageType) + " (" + paramString + ")");

            MemoryStream msgStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(msgStream);
            msgStream.Position = 0;

            //Are we replying to a specific request?
            if (replyID > 0)
                messageID = replyID;
            else
                messageID = DateTime.Now.Ticks;

            writer.Write(Constants.BOM + (byte)messageType + "~" + messageID + "~" + paramString + Constants.EOM);
            return msgStream;
        }
    }
}
